//
//  ListenListTopBannerView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/25.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit
import FSPagerView
import SDWebImage

protocol ListenListTopBannerViewDelegate:NSObjectProtocol {
    func ListenListTopBannerViewClick(cSrc:String, isH5:Int)
}

class ListenListTopBannerView: UIView {
    weak var delegate:ListenListTopBannerViewDelegate?
    
    var banner: [CommonBannerListModel]?
    
    // MARK: - 懒加载滚动图片浏览器
    private lazy var pagerView : FSPagerView = {
        let pagerView = FSPagerView()
        pagerView.delegate = self
        pagerView.dataSource = self
        pagerView.automaticSlidingInterval =  3
        pagerView.isInfinite = true
        pagerView.interitemSpacing = 0
        pagerView.transformer = FSPagerViewTransformer(type: .zoomOut)
        pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "HomeBannerCell")
        return pagerView
    }()
    
    private lazy var pagerControl:FSPageControl = {
        let pageControl = FSPageControl()
        pageControl.contentHorizontalAlignment = .center
        pageControl.setStrokeColor(WhiteBackColor, for: .normal)
        pageControl.setStrokeColor(TabbarTintColor, for: .selected)
        pageControl.setFillColor(UIColor.clear, for: .normal)
        pageControl.setFillColor(UIColor.clear, for: .selected)
        pageControl.setPath(UIBezierPath.init(roundedRect: CGRect.init(x: 0, y: 0, width: 5, height: 5), cornerRadius: 4.0), for: .normal)
        pageControl.setPath(UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 5, height: 5)), for: .selected)
        return pageControl
        
        }()
    
    var bannerList: [CommonBannerListModel]? {
        didSet {
            guard let model = bannerList else { return }
            self.banner = model
            self.pagerControl.numberOfPages = model.count
            self.pagerView.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(self.pagerView)
        self.addSubview(self.pagerControl)
        self.pagerView.frame = FRAME_CGFloat(0, 0, frame.size.width, frame.size.height)
        self.pagerControl.frame  = FRAME_CGFloat(0, frame.size.height - 15, frame.size.width, 15)
//        self.pagerView.itemSize = CGSize.init(width: frame.size.width, height: frame.size.height)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ListenListTopBannerView: FSPagerViewDelegate, FSPagerViewDataSource {
    // MARK:- FSPagerView Delegate
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.banner?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "HomeBannerCell", at: index)
        cell.imageView?.sd_setImage(with: URL(string:(StringIncludeChineseToString(self.banner?[index].imageUrl ?? ""))), completed: nil)
        
        return cell
    }
    
    func pagerViewWillEndDragging(_ pagerView: FSPagerView, targetIndex: Int) {
        pagerControl.currentPage = targetIndex
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
//        let clickUrl:String = self.banner?[index].clickUrl ?? "0"
//        let target:String = self.banner?[index].target ?? "0"
        let pro: String = self.banner?[index].cSrc ?? "0"
        let h5: Int = self.banner?[index].isH5 ?? 0
        delegate?.ListenListTopBannerViewClick(cSrc: pro, isH5: h5)
    }
}
